package com.gdufe.product.controller;

import java.util.Map;

import com.gdufe.common.vo.ReduceStockVo;
import com.gdufe.product.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gdufe.common.entity.Product;
import com.gdufe.product.service.ProductUserService;
import com.gdufe.common.utils.PageUtils;
import com.gdufe.common.utils.R;


/**
 * @author honghu
 * @email 3027708213@qq.com
 * @date 2023-10-17 13:49:23
 */
@RestController
@RequestMapping("/product/user")
public class ProductUserController {
    @Autowired
    private ProductUserService infoService;

    //根据商品id获取该商品信息
    @PostMapping("/getProduct")
    public R userSelectById(@RequestBody Long id) {
        Product info = infoService.getById(id);

        return R.ok().setData(info);
    }

    /**
     * 扣减库存
     */
    @RequestMapping("/reduce")
    public R reduceStock(@RequestBody ReduceStockVo reduceStockVo){
        infoService.reductStock(reduceStockVo);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = infoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        Product info = infoService.getById(id);

        return R.ok().put("info", info);
    }

    //对商品表单进行初始化
    @RequestMapping("/init/{id}")
    public R init(@PathVariable("id") Integer id){
        ProductVo info = infoService.initForm(id);

        return R.ok().put("info", info);
    }

}
